package com.atguigu.activemq.queue_redelivery;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;

import javax.jms.*;


public class JmsConsumer {
    public static final String QUEUE_NAME="queue_redelivery";
    public static final String ACTIVEMQ_URL="tcp://124.70.84.192:61616";
    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //设置最大重传次数为3次
        RedeliveryPolicy redeliveryPolicy=new RedeliveryPolicy();
        redeliveryPolicy.setMaximumRedeliveries(3);
        activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);
        MessageConsumer messageConsumer = session.createConsumer(queue);
        System.out.println("2号消费者");
        while (true){
            TextMessage textMessage= (TextMessage)messageConsumer.receive(1000l);
            if(null!=textMessage){
                System.out.println(textMessage.getText());
            }else {
                break;
            }
        }
        //session.commit();故意取消提交
        messageConsumer.close();
        session.close();
        connection.close();
    }
}
